package cn.buildoncloud.labor.workermanapplet.business.modules.controller;

import cn.buildoncloud.labor.workermanapplet.business.modules.services.WorkerTeamAttendanceService;
import cn.buildoncloud.labor.workermanapplet.common.Constant;
import cn.buildoncloud.labor.workermanapplet.common.base.Result;
import cn.buildoncloud.labor.workermanapplet.common.page.Pagination;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 班组考勤
 * Description: cn.buildoncloud.labor.workermanapplet.business.modules.controller
 * Created by CuiZibo on 2018/12/3 17:04
 */
@RestController
@RequestMapping("/workerTeamAttendance")
public class WorkerTeamAttendanceController {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WorkerTeamAttendanceService workerTeamAttendanceService;

    /**
     * 项目所有的班组和该班组当天出勤的人数   新增分页
     * @param projectId
     * @param date
     * @param key
     * @return
     */
    @RequestMapping("/queryWorkerTeamAttendanceList")
    public Object queryWorkerTeamAttendanceList(@RequestParam String projectId,
                                                @RequestParam String date,
                                                @RequestParam(required = false) String key,
                                                @RequestParam(defaultValue = "1") Integer pageNumber,
                                                @RequestParam(defaultValue = "5") Integer pageSize

    ) {

        try {
            Pagination pagination = workerTeamAttendanceService.queryWorkerTeamAttendanceList(projectId, date, key,pageNumber,pageSize);
            return Result.success(Constant.API_OPERATION_SUCCESS, pagination);
        } catch (Exception e) {
            log.error("workerTeamAttendance/queryWorkerTeamAttendanceList:", e);
            return Result.error(Constant.API_OPERATION_FAILED);
        }
    }

    /**
     * 班组每当30天的出勤情况
     * @param projectId
     * @param workerTeamId
     * @param yearAndMonth
     * @return
     */
    @RequestMapping("/queryWorkerTeamAttendanceByMonth")
    public Object queryWorkerTeamMonthAttendance(@RequestParam String projectId,
                                                 @RequestParam String workerTeamId,
                                                 @RequestParam String yearAndMonth) {

        try {
            List<Map> list = workerTeamAttendanceService.queryWorkerTeamAttendanceByMonth(projectId, workerTeamId, yearAndMonth);
            return Result.success(Constant.API_OPERATION_SUCCESS, list);
        } catch (Exception e) {
            log.error("workerTeamAttendance/queryWorkerTeamAttendanceByMonth:", e);
            return Result.error(Constant.API_OPERATION_FAILED);
        }
    }

    /**
     * 查询班组下所有成员的考勤情况，并排序
     * @param projectId
     * @param workerTeamId
     * @param yymmdd
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/queryWorkerTeamMembersAttendance")
    public Object queryWorkerTeamMembersAttendance(@RequestParam String projectId,
                                                   @RequestParam String workerTeamId,
                                                   @RequestParam String yymmdd,
                                                   @RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
                                                   @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {

        try {
            Pagination list = workerTeamAttendanceService.queryWorkerTeamMembersAttendance(projectId, workerTeamId, yymmdd, pageNo, pageSize);
            return Result.success(Constant.API_OPERATION_SUCCESS, list);
        } catch (
                Exception e)

        {
            log.error("workerTeamAttendance/queryWorkerTeamMembersAttendance:", e);
            return Result.error(Constant.API_OPERATION_FAILED);
        }
    }

}
